﻿/*                              
   Copyright 2011 Nils Kopal, Christopher Krüger, Simone Sauer, Dennis Nolte, Uni Duisburg-Essen

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sharebert.SearchLibrary.Interfaces;
using Sharebert.SearchLibrary;
using System.Threading;

namespace SharebertSearchLibraryDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            new Program().StartClient();
            Console.ReadLine();
        }

        // Just for testing purposes, feel free to edit
        private void StartClient()
        {
            ISearchClient client = new SearchClient("127.0.0.1", 4000, "127.0.0.1", 5000);
            //ISearchClient client = new SearchClient("134.91.78.68", 4000, "127.0.0.1", 5000);
            
            client.Timeout = 5;
            client.SearchResultReceived += new EventHandler<SearchResultEventArgs>(HandleSearchResult);
            client.ConnectionLost += new EventHandler<EventArgs>(HandleConnectionLost);

            FileOffer[] files = new FileOffer[2];
            files[0] = new FileOffer("First file.mp3", 123578);
            files[1] = new FileOffer("filename with Ümläuts.mp3", 1231231223);
            try
            {
                Console.WriteLine("Sending file offer");
                client.Offer(files);

                Console.WriteLine("Sending search request");
                client.Search(".*\\.mp3");

                // For testing purposes
                //Console.WriteLine("Sending file offer");
                //client.Offer(files);
                //Console.WriteLine("Sending file offer");
                //Thread.Sleep(1000);
                //client.Offer(files);
                //Console.WriteLine("Sending file offer");
                //Thread.Sleep(1000);
                //client.Offer(files);
                //Console.WriteLine("Sending file offer");
                //Thread.Sleep(1000);
                //client.Offer(files);
                //Console.WriteLine("Sending file offer");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void HandleSearchResult(object sender, SearchResultEventArgs e)
        {
            Console.WriteLine("Search results: ");
            foreach (SearchResult result in e.ResultList)
            {
                Console.WriteLine(String.Format(" Client {0}:{1} offers the following files:", result.ClientIP, result.ClientPort));
                foreach(FileOffer file in result.OfferingFiles)
                {
                    Console.WriteLine(String.Format("   File: {0} (Size: {1})", file.Name, file.Size));
                }
            }
        }

        private void HandleConnectionLost(object sender, EventArgs e)
        {
            IClientHandler client = (IClientHandler)sender;
            Console.WriteLine(String.Format("Connection to {0} timed out!", client.ToString()));
        }
    }
}
